package com.lin.filegraph.utils.componentRule;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 16:09
 */

import com.lin.filegraph.utils.threshold.Threshold;
import com.lin.filegraph.utils.compdg.ComponentEdge;
import com.lin.filegraph.utils.compdg.ComponentGraph;
import com.lin.filegraph.utils.compdg.ComponentNode;

/**
 * 单独依赖
 */
public class SingleDepend {

    public static void unionSD(ComponentGraph graph) {
        boolean isUnion =false;
        for(ComponentEdge ce :graph.getAllComponentEdges()){
            ComponentNode cn1 = ce.getPreComponent();
            ComponentNode cn2 = ce.getPostComponent();
            //节点1为域节点，节点二仅被节点1依赖
            if(isSingleDepend(graph, cn1, cn2)&&cn1.isFeature()!=true&&cn2.isFeature()!=true){
//				System.out.println(cn1+" "+cn2);
                graph.mergeNodes(cn1, cn2);
                isUnion=true;
                break;
            }
        }
        if(isUnion){
            unionSD(graph);
        }
    }

    private static boolean isSingleDepend(ComponentGraph graph,ComponentNode pre,ComponentNode post){
        if((post.getAllFiles().size()+post.getAllFiles().size())<graph.getTotalSize()* Threshold.COMSCALE){
//		if(pre.isRegion()&&(!post.isRegion())){
            if(graph.getPostComponents(post).size()==0&&graph.getPreComponents(post).size()==1){
                return true;
            }

//		if(post.isRegion()&&(!pre.isRegion())){
            if(graph.getPostComponents(pre).size()==1&&graph.getPreComponents(pre).size()==0){
                return true;
            }
        }
        return false;
    }
}

